﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SCADA_SANEST.Models;
using System.Collections.Generic;

namespace SCADA_SANEST.Services
{
    public static class ServiceManager
    {
        private static readonly Dictionary<string, PlcBaseService> _services = new();

        //Đoạn này mới sửa !!!
        public static PlcBaseService GetOrRegisterService(string key, PlcBaseService service)
        {
            if (!_services.ContainsKey(key))
            {
                _services[key] = service;
                service.Start();
            }
            return _services[key];
        }

        //public static void RegisterService(string key, PlcBaseService service)
        //{
        //    if (!_services.ContainsKey(key))
        //    {
        //        _services[key] = service;
        //        service.Start();
        //    }
        //}

        public static PlcBaseService GetService(string key)
        {
            return _services.ContainsKey(key) ? _services[key] : null;
        }

        public static void StopAll()
        {
            foreach (var service in _services.Values)
            {
                service.Stop();
            }
        }
    }
}